package w83b.w83bOperacionesPaso.w83bActas;

import java.io.InputStream;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;


import w83b.w83bBeans.W83bDocBean;

import es.ejie.frmk.presentacion.operacion.paso.Q70ActionPasoOP;
import es.ejie.frmk.presentacion.sesiondatos.Q70ContextoEJIE;

public class W83bDescargarDocumentoOutOPAction extends Q70ActionPasoOP{

	
	protected void processOP(Q70ContextoEJIE contexto, ActionErrors errores, ActionMapping mapping,
			ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
		//Guardamos la accion en el contexto
	}

	protected ActionForward processForward(Q70ContextoEJIE contexto, ActionErrors errors, ActionMapping mapping, 
			ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
			
			ServletOutputStream out = response.getOutputStream();
			W83bDocBean documento=(W83bDocBean)contexto.get("w83bDocBean");    
	        
	        InputStream inStream = documento.getFileDataBlob().getBinaryStream();
	        
	        // get the length of the blob
            long length = documento.getFileDataBlob().length();
            // read the blob into a byte array 
            // then print the blob from the array
            byte bytes[] = documento.getFileDataBlob().getBytes(1, new Long(length).intValue());
      
            //InputStream inStream = documento.getFileDataBlob().getBinaryStream();
        
            response.setContentType("application/octet-stream");
            response.setHeader("Content-Disposition","attachment;filename=\""+documento.getNombre()+"\"");

            
            /* byte[] outputByte = new byte[4096];
        	while(inStream.read(bytes, 0, 4096) != -1){
            out.write(bytes, 0, 4096);
        	}
        	inStream.close();*/
            out.write(bytes);

	        inStream.close();
	        out.flush();
	        out.close();
	
			return null;
	}
}
